package cn.edu.hqu.cst.lzs.homework_3_cx;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    TextView tv_show_action;
    Button btn_activity_as_listener;
    Button btn_inner_class_as_listener;
    Button btn_anonymous_class_as_listener;
    Button btn_outer_class_as_listener;
    Button btn_lambda_expression_as_listener;
    Button btn_show_system_info;
    Button btn_show_progress_bar;

    private int[] data = new int[100];
    int hasData = 0;
    final int PROGRESS_DIALOG = 0x112;
    int progressStatus = 0;
    ProgressDialog pd;
    Handler handler;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        tv_show_action=findViewById(R.id.tv_main_activity_show_action);
        btn_activity_as_listener=findViewById(R.id.btn_main_activity_activity_as_listener);
        btn_inner_class_as_listener=findViewById(R.id.btn_main_activity_inner_class_as_listener);
        btn_anonymous_class_as_listener=findViewById(R.id.btn_main_activity_anonymous_class_as_listener);
        btn_lambda_expression_as_listener=findViewById(R.id.btn_main_activity_lambda_expression_as_listener);
        btn_outer_class_as_listener=findViewById(R.id.btn_main_activity_outer_class_as_listener);
        btn_show_system_info=findViewById(R.id.btn_main_activity_show_system_info);
        btn_show_progress_bar=findViewById(R.id.btn_main_activity_show_progressbar);
        btn_activity_as_listener.setOnClickListener(this);
        btn_inner_class_as_listener.setOnClickListener(new InnerClassListener());

        btn_anonymous_class_as_listener.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_show_action.setText(R.string.str_anonymous_class_as_listener);
            }
        });

        btn_lambda_expression_as_listener.setOnClickListener(view->tv_show_action.setText(R.string.str_lambda_expression_as_listener));
        btn_outer_class_as_listener.setOnClickListener(new OuterClassListener(tv_show_action));

        btn_show_system_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,SystemInfoActivity.class);
                startActivity(intent);
            }
        });

        btn_show_progress_bar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View source) {
                progressStatus = 0;
                data = new int[100];
                hasData = 0;
                showDialog(PROGRESS_DIALOG);
            }
        });

        handler = new Handler() {
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0x123) {
                    pd.setProgress(msg.arg1);
                }
            }
        };

    }

    @Override
    public void onClick(View v) {
        tv_show_action.setText(R.string.str_activity_as_listener);
    }

    class InnerClassListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            tv_show_action.setText(R.string.str_inner_class_as_listener);
        }
    }

    public void clickHandler(View v){
        tv_show_action.setText(R.string.str_bind_to_label);
    }

    @Override
    public Dialog onCreateDialog(int id, Bundle status) {
        switch (id) {
            case PROGRESS_DIALOG:
                pd = new ProgressDialog(this);
                pd.setMax(100);
                pd.setTitle("任务完成百分比");
                pd.setMessage("耗时任务的完成百分比");
                pd.setCancelable(false);
                pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pd.setIndeterminate(false);
                break;
        }
        return pd;
    }

    public void onPrepareDialog(int id, Dialog dialog) {
        super.onPrepareDialog(id, dialog);
        switch (id) {
            case PROGRESS_DIALOG:
                pd.incrementProgressBy(-pd.getProgress());

                new Thread(() -> {
                    while (progressStatus < 100) {
                        progressStatus = doWork();
                        Message msg = new Message();
                        msg.what = 0x123;
                        msg.arg1 = progressStatus;
                        handler.sendMessage(msg);
                    }
                    if (progressStatus >= 100) {
                        pd.dismiss();
                    }
                }).start();
                break;
        }
    }

    public int doWork() {
        data[hasData++] = (int) (Math.random() * 100);
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return hasData;
    }

}


class OuterClassListener implements View.OnClickListener {
    TextView tvShowText;
    public OuterClassListener(TextView tvShowText){
        this.tvShowText=tvShowText;
    }
    @Override
    public void onClick(View v) {
        tvShowText.setText(R.string.str_outer_class_as_listener);
    }
}